/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fl.location.ui;


import fl.location.core.MobileLocation;
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author Zeor
 */
public class MainMenu implements Frame, CommandListener {

    private Sprite[] menuItems = new Sprite[3];
    private Sprite background;
    private LayerManager layerManager;
    private int cursorY = 0;
    private Command backCommand = new Command("Back", Command.OK, 0);
    private Command selectCommand = new Command("Select", Command.OK, 0);
    
    private int currentSelected;
    private int lastSelected;
    private int lastKey;
    
    MobileLocation mLoc;
    
    public MainMenu(MobileLocation mLoc)
    {
        this.mLoc = mLoc;

        layerManager = new LayerManager();
        currentSelected = 0;
        try {
            background = new Sprite(Image.createImage("/menu_background.gif"));
            background.setPosition(0, 0);
            menuItems[0] = new Sprite(Image.createImage("/menu1." + 1 + ".png"), 160, 27); 
            menuItems[1] = new Sprite(Image.createImage("/menu1." + 1 + ".png"), 160, 27);
            menuItems[2] = new Sprite(Image.createImage("/menu1." + 1 + ".png"), 160, 27);
            
            this.setMenuItems(menuItems);

        } catch (Exception ioex) {
            System.err.println(ioex);
        }
    }
    
    protected void setMenuItems(Sprite[] menuItems) {
        
        
        this.menuItems = menuItems;
        int offsetX = 120;
        int offsetY = 120;

        layerManager = new LayerManager();

        for (int i = 0; i < menuItems.length; i++) {
            layerManager.append(menuItems[i]);
            menuItems[i].setPosition(offsetX - (menuItems[i].getWidth() >> 1), offsetY + i * menuItems[i].getHeight());
        }

        layerManager.append(background);
        menuItems[0].setFrame(1);
    }


    public void draw(Graphics g) {
        layerManager.paint(g, 0, 0);
    }

    public void handleInput(int keyState) {
        
        lastSelected = currentSelected;
        if ((keyState & GameCanvas.UP_PRESSED) != 0) {
            if((lastKey & GameCanvas.UP_PRESSED) == 0){
                if (currentSelected > 0) {
                    currentSelected--;
                } else {
                    currentSelected = menuItems.length - 1;
                }

                menuItems[lastSelected].setFrame(0);
                menuItems[currentSelected].setFrame(1);
            }
        } else if ((keyState & GameCanvas.DOWN_PRESSED) != 0) {
            if((lastKey & GameCanvas.DOWN_PRESSED) == 0){
                if (currentSelected < (menuItems.length - 1)) {
                    currentSelected++;
                } else {
                    currentSelected = 0;
                }

                menuItems[lastSelected].setFrame(0);
                menuItems[currentSelected].setFrame(1);
            }
        } else if ((keyState & GameCanvas.FIRE_PRESSED) != 0) {
            if(currentSelected == 0){
                mLoc.setFrame(mLoc.MAP);
            }
        }
        lastKey = keyState;
    }

    public void commandAction(Command arg0, Displayable arg1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
